package br.com.tamagochi.view;


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Allan & Fabiana
 */
public class GifPanel extends JPanel {

    private BufferedImage bf;
    private Image img;
    private Graphics2D g2d;
    private MediaTracker tracker;
    
    public GifPanel(){
        
        this.setSize(417, 417);
        
        this.setVisible(true);
    }
    
    @Override
    public void paint(Graphics g) {
        //super.paint(g);
        if (this.img != null) {
            bf = (BufferedImage) createImage(this.getWidth(),this.getHeight());

            g2d = bf.createGraphics();
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

            g2d.clearRect(0, 0, bf.getWidth(), bf.getHeight());

            g2d.drawImage(img, 0, 0, this);

            g2d.dispose();

            g.drawImage(bf, 0, 0, this);

        }
        
    }
    
    public void setImage(Image img){
        this.img = img;
        
        tracker = new MediaTracker(this);
        
        try {
            tracker.addImage(img, 0);
            tracker.waitForID(0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        repaint();
    }
    
}
